/*
 *  FILE: 
 *  	xdirent.h
 *
 *  DESCRIPTION:
 *  	A set of functions used for loading a directory structure into
 *      a "struct xdirent *" array.   EVERY call of xdirent_list MUST
 *      be followed by a xdirent_list_free, the number of members is required!
 *
 *  FUNCTIONS:
 *      xdirent_alphasort()   - Portable alphasort rewrite. uses string_cmp from local string.h
 *      xdirent_r_alphasort() - "Reversed" xdirent_alphasort(). 
 *  	xdirent_list()        - Fill array of dirents using directory fd.
 *  	xdirent_list_free()   - Free any data allocated by dirent_list().
 *
 *
 *
 * Copyright (c) 2004-2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "string.h" /*string_cmp needed from here, also, strlcpy and strlcat on Linux/Windows*/


/*I cannot count on the memory involving system specific dirents. 
 *all xdirent functions should use the xdirent struct.
 *NOTE: d_name is only the size of the string+1 copied into it*/
struct xdirent {
       char * d_name;
     #ifndef __BSDSYSTEMS__
       ino_t  d_ino;
     #endif
       };

#ifndef MAX_PATH 
        #define MAX_PATH 260
#endif

#ifndef PATH_MAX 
        #define PATH_MAX MAX_PATH
#endif

#ifdef WIN32
       #ifndef __WIN32__
               #define __WIN32__
       #endif
#endif


#include <dirent.h>


int signed xdirent_alphasort (const void *a, const void *b);
int signed xdirent_r_alphasort(const void *a, const void *b);

/* NOTE: directory must be OPENED AND READY FOR READING! */
size_t xdirent_list(DIR * dirfd, struct xdirent ** dirent_struct);

/*cleans up dirent_list dirent data(ie frees alloced data)*/
size_t xdirent_list_free(struct xdirent ** dirent_struct, size_t nmebs);
